package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entity.BeanConv;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Consumer;

/**
 * @author ：yuanjian
 * @date ：Created in 2020/11/11 14:54
 * @description：
 */
@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Override
    public void add(CheckGroupDto checkGroupDto) {
        CheckGroup checkGroup = BeanConv.toBean(checkGroupDto, CheckGroup.class);
//        save(checkGroup);
        baseMapper.add(checkGroup);
        Long groupId = checkGroup.getId();
       /* List<Long> checkitemIds = checkGroupDto.getCheckitemIds();
        if (checkitemIds != null && checkitemIds.size() != 0) {
            for (Long checkitemId : checkitemIds) {
                baseMapper.addCheckGroupAndCheckItemIds(checkitemId, groupId);
            }
        }*/
    }


    //修改group，在删除原来的联系表，在添加联系表
    @Override
    @Transactional
    public void save(CheckGroupDto checkGroupDto) {
        CheckGroup checkGroup = BeanConv.toBean(checkGroupDto, CheckGroup.class);
        //如果执行添加则groupId为null，执行修改则groupId不为null
        Long groupId = checkGroup.getId();
        List<Long> checkitemIds = checkGroupDto.getCheckitemIds();
        //执行修改操作1.删除联系表中相关数据
        if (checkGroup.getId() != null) {
            baseMapper.deleteCheckGroupAndCheckItemIds(groupId);
        }
        //执行添加操作1.添加group信息
        if (groupId == null) {
            baseMapper.add(checkGroup);
        }//执行更新操作2.修改group信息
        else {
            saveOrUpdate(checkGroup);
        }
        //添加和修改操作的最后一步都是：添加相关联系表的数据
        if (checkitemIds != null && checkitemIds.size() != 0) {
            for (Long checkitemId : checkitemIds) {
                baseMapper.addCheckGroupAndCheckItemIds(checkitemId, checkGroup.getId());
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return this.list(new QueryWrapper<CheckGroup>().eq("is_delete", 0));
    }

    @Override
    public boolean deleteById(Long id) {
        CheckGroup checkGroup = baseMapper.selectById(id);
        if (checkGroup != null)
            checkGroup.setIs_delete(1);
        return updateById(checkGroup);
    }

    @Override
    public List<CheckItem> findCheckItemsByCheckGroupId(Long checkGroupId) {
        return baseMapper.findCheckItemsByCheckGroupId(checkGroupId);

    }

    @Override
    public PageResult findByPage(QueryPageBean pageBean) {
        IPage<CheckGroup> page = new Page<>();
        //如果查询条件不为空或trim后不为null，执行条件查询
        if (!(StringUtils.isEmpty(pageBean.getQueryString()) && StringUtils.trimAllWhitespace(pageBean.getQueryString()) == null)) {
            //分页查询is_delete=0的数据，且 查询条件是name或code或helpcode三者的模糊查询
            page = page(new Page<>(pageBean.getCurrentPage(), pageBean.getPageSize()), new QueryWrapper<CheckGroup>().eq("is_delete", 0)
                    .and(wrapper -> {
                        wrapper.like("name", pageBean.getQueryString()).or()
                                .like("code", pageBean.getQueryString()).or()
                                .like("helpcode", pageBean.getQueryString());
                    })
            );

        } else {
            page = page(new Page<>(pageBean.getCurrentPage(), pageBean.getPageSize()), new QueryWrapper<CheckGroup>().eq("is_delete", 0));

        }
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public List<Long> findCheckItemByCheckGroupId(Long checkGroupId) {
        return baseMapper.findCheckItemByCheckGroupId(checkGroupId);
    }


}
